﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Home.Haushalt.Lib;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinTabControl;
using Home.Haushalt.Lib.Element;
using Infragistics.Win;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmPropertyFinAccount : FrmProperty
    {
        /// <summary></summary>
        private UcGridFormElement ucFinDateGridForm;

        /// <summary></summary>
        private FinDate _elementObj;

        /// <summary>
        /// 
        /// </summary>
        public FrmPropertyFinAccount()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPropertyFinAccount_Load(object sender, EventArgs e)
        {
            _elementObj = new FinDate((FinAccount)ElementObject);

            UltraTab tab = new UltraTab();
            tab.Text = _elementObj.GetType().Name.ToString();
            this.tabControl.Tabs.Add(tab);

            ucFinDateGridForm = new UcGridFormElement(_elementObj, DefaultColumnSets.GridDefault, DefaultColumnSets.GridDefault.ToString());
            ucFinDateGridForm.Dock = DockStyle.Fill;
            tab.TabPage.Controls.Add(ucFinDateGridForm);
            ucFinDateGridForm.BaseGrid.DisplayLayout.CaptionVisible = DefaultableBoolean.False;
            ucFinDateGridForm.BaseGrid.ContextMenuStrip.Opened += new EventHandler(ContextMenuStrip_Opened);
            ucFinDateGridForm.BaseGrid.OnElementDeleted += new EventHandler<HEventArgs>(BaseGrid_OnElementDeleted);
            tabControl.SelectedTab = tab;

            //Binding
            AddBinding(txtName.DataBindings, "Text", FinAccount.Fields.Name);
            AddBinding(txtBank.DataBindings, "Text", FinAccount.Fields.Bank);
            AddBinding(txtBlz.DataBindings, "Text", FinAccount.Fields.BLZ);
            AddBinding(txtAccount.DataBindings, "Text", FinAccount.Fields.Kto);
            AddBinding(txtRefAccount.DataBindings, "Text", FinAccount.Fields.RefKto);
            AddBinding(txtSaldo.DataBindings, "Text", FinAccount.Fields.Saldo);
            AddBinding(txtLastUpdate.DataBindings, "Text", FinAccount.Fields.LastUpdate);
            AddBinding(txtNewDateCount.DataBindings, "Text", FinAccount.Fields.LastChangedCount);
            AddBinding(txtPin.DataBindings, "Text", FinAccount.Fields.PIN);

            AddBinding(cbBaseAccount.DataBindings, "Checked", FinAccount.Fields.IsBaseKonto);
            AddBinding(cbUpdating.DataBindings, "Checked", FinAccount.Fields.Updating);

            DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BaseGrid_OnElementDeleted(object sender, HEventArgs e)
        {
            _elementObj.ElementTable.DataSet.WriteXml(_elementObj.FileName, DAO.Credential.CurrentCry);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ContextMenuStrip_Opened(object sender, EventArgs e)
        {
            if (ucFinDateGridForm.BaseGrid._clickedObject is UltraGridCell || ucFinDateGridForm.BaseGrid._clickedObject is UltraGrid)
            {
                ucFinDateGridForm.BaseGrid.ContextMenuCopyElementItem.Visible = false;
                //ucFinDateGridForm.BaseGrid.ContextMenuDeleteElementItem.Visible = false;
                ucFinDateGridForm.BaseGrid.ContextMenuNewElementItem.Visible = false;


                //int insertIndex = 0;

                //string elementCaption = (ucFinDateGridForm.BaseGrid.Selected.Rows.Count > 1) ? LO.ElementList(Page.ElementType) : LO.ElementName(Page.ElementType);

                //ToolStripMenuItem ContextMenuDeleteElementItem = new ToolStripMenuItem(LO.GridContextMenu("Element.Delete", elementCaption), global:: Home.Haushalt.Lib.Properties.Resources.element_delete, cmnuPropertyDelete_Click);
                //ContextMenuDeleteElementItem.Enabled = (ucFinDateGridForm.BaseGrid.Selected.Rows.Count > 0);
                //this.ContextMenuStrip.Items.Add(ContextMenuDeleteElementItem);
            }
        }


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void cmnuPropertyDelete_Click(object sender, EventArgs e)
        //{
        //    if (MessageBox.Show("Markierte Einträge inkl. Unterobjekte löschen?", "Einträge löschen", MessageBoxButtons.YesNo) == DialogResult.Yes)
        //    {
        //        DeleteRows(SelectedRows);
        //    }
        //}
    }
}
